package psl;

import java.sql.*;
import java.text.*;
import java.util.*;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: phiteshkumar
 * Date: Feb 23, 2007
 * Time: 2:19:16 PM
 */
public class CommonServices
{
    public static void throwError (Exception feException)
    {
        Error loError = new Error ("\r\n" + feException.toString ());
        loError.setStackTrace (feException.getStackTrace ());
        throw loError;
    }

    public static void throwError (String fsMessage, Exception feException)
    {
        Error loError = new Error ("\r\n" + feException.toString () + fsMessage + "\r\n");
        loError.setStackTrace (feException.getStackTrace ());
        throw loError;
    }

    public static String[] stringToArray (String fsString, String fsDelimiter)
    {
        if (fsString == null)
        {
            return new String[0];
        }
        int liPos, liStart = 0, liLen;
        ArrayList lvData = new ArrayList ();

        liLen = fsDelimiter.length ();
        liPos = fsString.indexOf (fsDelimiter, liStart);

        while (liPos > -1)
        {
            lvData.add (fsString.substring (liStart, liPos).trim ());
            liStart = liPos + liLen;
            liPos = fsString.indexOf (fsDelimiter, liStart);
        }
        lvData.add (fsString.substring (liStart).trim ());

        int liSize = lvData.size ();
        String[] lsArray = new String[liSize];
        lvData.toArray (lsArray);
        return lsArray;
    }

    public static boolean isDate (String fsNum)
    {
        try
        {
/*
            SimpleDateFormat  loSimpleDateFormat =new SimpleDateFormat ("dd-MMM-yyyy");
*/
            Date lsDate=AppConstants.SIMPLE_DATE_FORMAT.parse (fsNum);
        }
        catch(ParseException ef)
        {
/*
            ef.printStackTrace ();
*/
            return false;
        }

        return true;
    }
    public static boolean isNaN (String fsNum)
    {
        int liAsci = 0;
        String lsNum = fsNum;

        if (!lsNum.equals (""))
        {
            int liAccount = lsNum.length ();
            for (int liCnt = 0; liCnt < liAccount; liCnt++)
            {
                char lcPhone = lsNum.charAt (liCnt);
                liAsci = lcPhone;

                if ((liAsci < 48) || (liAsci > 57))
                {
                    return true;
                }
            }
        }
        return false;
    }

    public static String[] ArrayListToString (ArrayList fvArrayList)
    {
        Iterator lItr = fvArrayList.iterator ();
        String[] lsStringArray = new String[fvArrayList.size ()];
        int count = 0;
        while (lItr.hasNext ())
        {
            lsStringArray[count] = (String) lItr.next ();
            count++;
        }
        System.err.println ("lsStringArray: "+lsStringArray[0]);
        return lsStringArray;
    }

    public static String  getMaxNoFormDB(String lsTableName,String lsColumnName, String lsCondition, String  lsCharecter)
    {
        String lsQuery="Select max("+lsColumnName+") From "+lsTableName;
/*
        String lsQuery="Select max(to_number(substr("+lsColumnName+",2))) From "+lsTableName;
*/
        if(!lsCondition.equals (""))
        {
            lsQuery=lsQuery+" Where "+lsCondition;
        }
//        System.err.println("lsQuery:::::"+lsQuery);
        ResultSet loResultSet=null;
        loResultSet = DBConn.getRecordFromDB (lsQuery);
//        System.err.println("loResultSet:::::"+loResultSet);
        int licount=0;
        String lsId=null;
        try
        {
            if(loResultSet!=null)
            {
                while(loResultSet.next ())
               {
                  licount=loResultSet.getInt (1);
               }
            }
           if(licount==0)
           {
              lsId= lsCharecter+"1";
           }
            else
           {
               lsId=lsCharecter+(licount+1);
           }
        }
        catch(Exception e)
        {
            e.printStackTrace ();
        }
     return lsId;
    }
    public static boolean isEmail (String fsEmail)
    {
        int length = fsEmail.length ();
        int Position1 = fsEmail.indexOf ('@');
        int Position2 = fsEmail.indexOf ('.');

        if (Position1 == 0 || Position1 == -1)
        {
            return false;
        }
        else if (Position2 == 0 || Position2 == -1 || Position2 == length)
        {
            return false;
        }
        return true;
    }
}
